@ECHO OFF
::===[v1.0.0]===================================================================
:: Load variables from file and generate rc file. Variables should be prefixed
:: with "VER_". Uses brcc32 to compile RC file.
:: List of used variables:
:: - Integer.
::   VER_Major, VER_Minor, VER_Build, VER_Revision.
:: - String. Enclosed with quotes
::   VER_CompanyName, VER_LegalCopyright, VER_FileDescription,
::   VER_OriginalFilename, VER_InternalName, VER_ProductName.

SETLOCAL ENABLEDELAYEDEXPANSION
SET "ScriptName=%~n0%~x0"
CALL :Setup "VersionInfo.inc" "VersionInfo.rc"
CALL :ReadVariables %FileVariables%
CALL :GenerateFile
brcc32 "%FileOutput%"
EXIT /b


::===[Methods]==================================================================

:: Setup and format initial values.
:: %1 - "FileVariables" - File contains variables to be used for template.
:: %2 - "FileOutput" - Output filename.
:: All parameters should be enclosed in quotes. Spaces will be trimmed.
:Setup
    CALL :Trim FileVariables %~1
    CALL :Trim FileOutput %~2
EXIT /b

:: Generate RC file usingg variables from provided file.
:GenerateFile
    (
        ECHO ^/^/ Autogenerated file. Do not change.
        ECHO ^/^/ Input variables file: "%FileVariables%"
        ECHO ^/^/ Generated by batch script: "%ScriptName%"
        ECHO.
        ECHO #define VER_PRIVATEBUILD              0
        ECHO.
        ECHO #ifndef RELEASE
        ECHO #define VER_PRERELEASE                VS_FF_PRERELEASE
        ECHO #else
        ECHO #define VER_PRERELEASE                0
        ECHO #endif
        ECHO.
        ECHO #ifndef DEBUG
        ECHO #define VER_DEBUG                     0
        ECHO #else
        ECHO #define VER_DEBUG                     VS_FF_DEBUG
        ECHO #endif
        ECHO.
        ECHO VS_VERSION_INFO VERSIONINFO
        ECHO FILEVERSION                           %VER_Major%,%VER_Minor%,%VER_Build%,%VER_Revision%
        ECHO PRODUCTVERSION                        %VER_Major%,%VER_Minor%,%VER_Build%,%VER_Revision%
        ECHO FILEFLAGSMASK                         VS_FFI_FILEFLAGSMASK
        ECHO FILEFLAGS                             ^(VER_PRIVATEBUILD ^| VER_PRERELEASE ^| VER_DEBUG^)
        ECHO FILEOS                                VOS_NT ^| VOS__WINDOWS32
        ECHO FILETYPE                              VFT_DLL
        ECHO FILESUBTYPE                           VFT2_UNKNOWN
        ECHO BEGIN
        ECHO     BLOCK "StringFileInfo"
        ECHO     BEGIN
        ECHO         BLOCK "000004E4"
        ECHO         BEGIN
        ECHO             VALUE "ProductVersion",   "%VER_Major%.%VER_Minor%.%VER_Build%.%VER_Revision%\0"
        ECHO             VALUE "FileVersion",      "%VER_Major%.%VER_Minor%.%VER_Build%.%VER_Revision%\0"
        ECHO             VALUE "CompanyName",      "%VER_CompanyName:"=%\0"
        ECHO             VALUE "FileDescription",  "%VER_FileDescription:"=%\0"
        ECHO             VALUE "InternalName",     "%VER_InternalName:"=%\0"
        ECHO             VALUE "LegalCopyright",   "%VER_LegalCopyright:"=%\0"
        ECHO             VALUE "LegalTrademarks",  "%VER_LegalTrademarks:"=%\0"
        ECHO             VALUE "OriginalFilename", "%VER_OriginalFilename:"=%\0"
        ECHO             VALUE "ProductName",      "%VER_ProductName:"=%\0"
        ECHO             VALUE "Comments",         "%VER_Comments:"=%\0"
        ECHO         END
        ECHO     END
        ECHO     BLOCK "VarFileInfo"
        ECHO     BEGIN
        ECHO         VALUE "Translation", 0x0000, 1252
        ECHO     END
        ECHO END
    ) > %FileOutput%
EXIT /b

:: Load variables from provided file.
:: %1 - Provided file
:: Loaded variables should start with VER_ prefix. Spaces from variable name
:: and value will be truncated.
:ReadVariables
    FOR /f "USEBACKQ TOKENS=1,2 DELIMS==;" %%a IN (%1) DO (
        SET var=%%a
        CALL :StartsWith var "VER_" result
        IF !result!==1 (
            CALL :Trim val %%b
            SET !var: =!=!val!
        )
    )
EXIT /b


:: ==[Utils]====================================================================

:: Trim trailing and leading spaces from provided string values.
:: %1 - Name of return variable.
:: %2 - String value to trim. Enclosing quotes will be removed.
:Trim
    SETLOCAL EnableDelayedExpansion
    SET Params=%*
    FOR /f "tokens=1*" %%a IN ("!Params!") DO ENDLOCAL & SET %1=%%b
EXIT /b

:: Tests if a text starts with a given token.
:: %1 - String to check.
:: %2 - Token. Enclosing quotes will be removed.
:: %3 - Returns 1 if text start with provided token. Otherwise returns 0.
:StartsWith
    SET "txt=!%~1!"
    SET "str=%~2"
    SET "%~3=1"
    IF DEFINED str CALL SET "s=%str%%%txt:*%str%=%%"
    IF /i "%txt%" NEQ "%s%" (SET "%~3=0")
EXIT /b